package com.hwadeefsc.springboot.controller;


import com.alibaba.fastjson.JSONArray;
import com.hwadeefsc.springboot.entity.Users;
import com.hwadeefsc.springboot.service.UsersService;
import com.hwadeefsc.springboot.utils.result.DataResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Users)表控制层
 *
 * @author makejava
 * @since 2024-07-09 21:43:27
 */
@RestController
@RequestMapping("users")
public class UsersController {
    /**
     * 服务对象
     */
    @Resource
    private UsersService usersService;

    /**
     * 分页查询
     *
     * @param users       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Users>> queryByPage(Users users, PageRequest pageRequest) {
        return ResponseEntity.ok(this.usersService.queryByPage(users, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Users> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.usersService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param users 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Users> add(Users users) {
        return ResponseEntity.ok(this.usersService.insert(users));
    }

    /**
     * 编辑数据
     *
     * @param users 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Users> edit(Users users) {
        return ResponseEntity.ok(this.usersService.update(users));
    }


    @DeleteMapping("/del/{userId}")
    public ResponseEntity<Boolean> deleteById(@PathVariable Integer userId) {
        boolean deleted = this.usersService.deleteById(userId);
        return ResponseEntity.ok(deleted);
    }


     @RequestMapping("/queryByType")
     public DataResult<JSONArray> queryByType(@RequestBody Users users){
          return DataResult.successByDataArray(this.usersService.queryByType(users.getRole().toString()));
     }

    /**
     * 删除数据
     *
     * @param item 实例对象
     * @return 删除是否成功
     */
    @PostMapping("/register")
    public ResponseEntity<Integer> registUser(@RequestBody Users item) {


        return ResponseEntity.ok(this.usersService.registUser(item));

    }

    /**
     * 查询全部数据
     *
     * @return 查询结果
     */
    @GetMapping("/all")
    public DataResult<JSONArray> queryAllUser(){

        return DataResult.successByDataArray(this.usersService.queryAllUser());
    }

}

